package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.AccidentDetail;
import cn.wolfcode.crm.domain.ClientCase;
import cn.wolfcode.crm.domain.Insurance;
import cn.wolfcode.crm.mapper.AccidentDetailMapper;
import cn.wolfcode.crm.mapper.ClientCaseMapper;
import cn.wolfcode.crm.mapper.InsuranceMapper;
import cn.wolfcode.crm.query.impl.AccidentDetailQuery;
import cn.wolfcode.crm.service.IAccidentDetailService;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Service
public class AccidentDetailServiceImpl implements IAccidentDetailService {
    @Autowired
    AccidentDetailMapper accidentDetailMapper;
    @Autowired
    InsuranceMapper insuranceMapper;
    @Autowired
    ClientCaseMapper clientCaseMapper;

    public void delete(Long id) {
        accidentDetailMapper.deleteByPrimaryKey(id);
    }

    public void saveOrUpdate(AccidentDetail record) {
        if (record.getId() != null) {
            accidentDetailMapper.updateByPrimaryKey(record);
            String numbering = record.getNumbering();
            Insurance insurance = insuranceMapper.selectBySn(numbering);
            if (record.getCheck() != null) {
                if (record.getCheck() == true) {
                    ClientCase clientCase = new ClientCase();
                    clientCase.setId(record.getId());
                    //设置发送状态为false
                    clientCase.setState(false);
                    //设置背锅侠
                    clientCase.setWok(record.getWok());
                    //设置保险机构
                    clientCase.setOrganization(insurance.getOrganization().getName());
                    //设置联系电话
                    clientCase.setTel(record.getTel());
                    //设置损失金额
                    clientCase.setLossmoney(record.getCompensate());
                    //设置事发地址
                    clientCase.setPlace(record.getPlace());
                    //设置客户名
                    clientCase.setClientname(insurance.getCustomer().getName());
                    //设置保单号
                    clientCase.setNumbering(record.getNumbering());
                    //设置车牌号
                    clientCase.setCarnumber(record.getCarnumber());
                    //设置事故时间
                    clientCase.setTime(record.getTime());
                    clientCaseMapper.insert(clientCase);
                    clientCaseMapper.updateByPrimaryKey(clientCase);
                }
            }
        } else {
            accidentDetailMapper.insert(record);
            String numbering = record.getNumbering();
            Insurance insurance = insuranceMapper.selectBySn(numbering);
            if (record.getCheck() == true) {
                ClientCase clientCase = new ClientCase();
                //设置发送状态为false
                clientCase.setState(false);
                //设置背锅侠
                clientCase.setWok(record.getWok());
                //设置保险机构
                clientCase.setOrganization(insurance.getOrganization().getName());
                //设置联系电话
                clientCase.setTel(record.getTel());
                //设置损失金额
                clientCase.setLossmoney(record.getCompensate());
                //设置事发地址
                clientCase.setPlace(record.getPlace());
                //设置客户名
                clientCase.setClientname(insurance.getCustomer().getName());
                //设置保单号
                clientCase.setNumbering(record.getNumbering());
                //设置车牌号
                clientCase.setCarnumber(record.getCarnumber());
                //设置事故时间
                clientCase.setTime(record.getTime());
                clientCaseMapper.insert(clientCase);
            }
        }
    }

    public AccidentDetail get(Long id) {
        return accidentDetailMapper.selectByPrimaryKey(id);
    }

    public List<AccidentDetail> getAll() {
        return accidentDetailMapper.selectAll();
    }


    public PageResult query(AccidentDetailQuery qo) {
        int rows = accidentDetailMapper.queryForCount(qo);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<AccidentDetail> result = accidentDetailMapper.queryForList(qo);
        return new PageResult(rows, result);
    }

    public void changeState(Long id) {
        AccidentDetail detail = accidentDetailMapper.selectByPrimaryKey(id);
        if (detail.getCardnum() == null || detail.getCompensate() == null || detail.getCarnumber() == null || detail.getEmployee() == null || detail.getNumbering() == null
                || detail.getPayee() == null || detail.getSummary() == null || detail.getTel() == null || detail.getWok() == null
                || detail.getPlace() == null || detail.getTips() == null) {
            throw new LogicException("表单不完整,清完善表单");
        }
        accidentDetailMapper.changeState(id);
        detail.setCheck(true);
        detail.setState(true);
        saveOrUpdate(detail);
    }

    public PageResult queryByState(AccidentDetailQuery qo) {
        int rows = accidentDetailMapper.queryForCountByState(qo);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<AccidentDetail> result = accidentDetailMapper.queryForListByState(qo);
        return new PageResult(rows, result);
    }

    public void changeCheckState(Long id) {
        //1.先拿到对应的保单
        AccidentDetail detail = accidentDetailMapper.selectByPrimaryKey(id);
        String numbering = detail.getNumbering();
        Insurance insurance = insuranceMapper.selectBySn(numbering);
        BigDecimal bigDecimal = new BigDecimal(detail.getCompensate());
        if (insurance.getTotalAmount().compareTo(bigDecimal) == -1) {
            throw new LogicException("保额不足,请升级保单或者修改保额");
        }
        saveOrUpdate(detail);
    }

    public PageResult queryByCheck(AccidentDetailQuery qo) {
        int rows = accidentDetailMapper.queryForCountByCheck(qo);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<AccidentDetail> result = accidentDetailMapper.queryForListByCheck(qo);
        return new PageResult(rows, result);
    }
}
